"""
Use class FittableLabel or in yours
FittableLable object inherits Lable and
FittableButton object inherits Button adding folowing attributes:
length: (positive int) max length of content (default max(len(self.text),1))
fit_height: (bool) whether widget should fit font_size to its height
            (default True)
coef: (positive float) (less than 1) calculated font-size is multiplied
      on this attribute

look in add_words.kv file for example
"""

from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.lang import Builder

from kivy.properties import BoundedNumericProperty, BooleanProperty


Builder.load_string('''
<FittableLabel>:
    font_size: self.coef*min(self.size[0]/self.length*1.4, \
                   self.size[1]*0.6 if self.fit_height else 'inf')
    length: max(len(self.text),1)
<FittableButton>:
    font_size: self.coef*min(self.size[0]/self.length*1.4, \
                   self.size[1]*0.6 if self.fit_height else 'inf')
    length: max(len(self.text),1)
<FittableToggleButton>:
    font_size: self.coef*min(self.size[0]/self.length*1.4, \
                   self.size[1]*0.6 if self.fit_height else 'inf')
    length: max(len(self.text),1)
''')


class FittableLabel(Label):
    length = BoundedNumericProperty(10, min=1)
    coef = BoundedNumericProperty(1, min=0, max=1)
    fit_height = BooleanProperty(True)

    def __init__(self, **kwargs):
        super(FittableLabel, self).__init__(**kwargs)


class FittableButton(Button):
    length = BoundedNumericProperty(10, min=1)
    coef = BoundedNumericProperty(1, max=1)
    fit_height = BooleanProperty(True)

    def __init__(self, **kwargs):
        super(FittableButton, self).__init__(**kwargs)


class FittableToggleButton(ToggleButton):
    length = BoundedNumericProperty(10, min=1)
    coef = BoundedNumericProperty(1, max=1)
    fit_height = BooleanProperty(True)

    def __init__(self, **kwargs):
        super(FittableToggleButton, self).__init__(**kwargs)
